package org.yufan.admin.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.yufan.admin.result.EasyUIResult;
import org.yufan.admin.service.ItemDescService;
import org.yufan.admin.service.ItemService;
import org.yufan.core.bean.Item;
import org.yufan.core.bean.ItemDesc;
import org.yufan.core.result.Result;
import org.yufan.core.result.ResultUtils;
import org.yufan.core.result.StatusCode;


/**
 * Created by zxd on 2019/3/5
 **/
@RequestMapping("/item")
@ResponseBody
@Controller
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Autowired
    private ItemDescService itemDescService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(Item item, String desc){
        itemService.insertItem(item,desc);
        return ResultUtils.buildSuccess();
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(Item item, String desc){
        itemService.updateItem(item,desc);
        return ResultUtils.buildSuccess();
    }

    @RequestMapping(value = "/reshelf",method = RequestMethod.POST)
    public Result reshelf(String ids){
        if(StringUtils.isEmpty(ids)){
            return ResultUtils.buildFail(StatusCode.IDS_NOT_EMPTY);
        }
        itemService.updateReshelf(ids);
        return ResultUtils.buildSuccess();
    }


    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public EasyUIResult list(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer rows){
        PageInfo<Item> pageInfo = itemService.findByPageWhere(null, page, rows);
        return new EasyUIResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @RequestMapping(value = "/desc/{itemId}",method = RequestMethod.GET)
    public ResponseEntity desc(@PathVariable("itemId") Long itemId){
        ItemDesc itemDesc = itemDescService.findById(itemId);
        return ResponseEntity.ok(ResultUtils.buildSuccess(itemDesc));
    }


}
